﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO: 1、可以选择当前时间的前后天数
    ///       2、支持更多的时间格式，如月-日
    /// </summary>
    public class AddReduceTimeNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            AddReduceTimeNParam? param = item.ControlParam as AddReduceTimeNParam;
            string outVarName = param.OutVarName;
            this.GetStringVar(ref outVarName, globleData);

            string dateobj = param.DateObj;
            DateTime operateTarget = this.GetObjVar<DateModel>(ref dateobj, globleData).DateValue;

            int adjustType = param.AdjustType;

            string adjustStepString = param.AdjustStep;
            this.GetStringVar(ref adjustStepString, globleData);
            int adjustStep = int.Parse(adjustStepString);

            int timeUnit = param.TimeUnit;

            if(adjustType != 0) {
                //减少
                adjustStep = -adjustStep;
            }

            DateTime adjustTime;
            switch(timeUnit) {
                case 0:
                    adjustTime = operateTarget.AddDays(adjustStep);
                    break;
                case 1:
                    adjustTime = operateTarget.AddMonths(adjustStep);
                    break;
                default:
                    adjustTime = operateTarget.AddYears(adjustStep);
                    break;
            }

            this.saveVar(globleData, outVarName, new DateModel(outVarName, adjustTime), DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
